
#pragma once
#include "rental_admin.h"
#include "movies_admin.h"
#include "movie_title.h"
#include "accounts_admin.h"
#include <iostream>
#include <conio.h>

rental_admin *rental_admin::instance=NULL;

using namespace std;
rental_admin::rental_admin()
{
	rentals_counter=0;
};

rental_admin * rental_admin::get_rental_admin()
	{
		if (instance==NULL) instance = new rental_admin();
		return instance;
	};

/*
rent function

returns 0 if there is no available cds or customer is not available to rent
returns 1 if there is available cd and customer is available to rent
*/

bool rental_admin::rent(string movie, int phone)
{
	// prepare available cd to be rented

	movies_admin *movies_admin_ = movies_admin::get_movies_admin();
	if (!movies_admin_->search(movie))
	{
		cout<<endl<<"The movie "<<movie<<" is not existed"<<endl<<endl;
		return 0;
	}
	if 	(!movies_admin_->check_availability(movie))
	{
		cout<<endl<<"Sorry there is no available copies"<<endl<<endl;
		return 0;
	}
	movie_cd * rented_movie_cd = movies_admin_->get_available_cd(movie);

	// prepare member object , or create a non-member temporary object

	accounts_admin * accounts_admin_ = accounts_admin::get_accounts_admin();
	customer *tenant = accounts_admin_->get_tenant(phone);

	if (!accounts_admin_->check_availability(phone))
	{
		cout<<endl<<"Sorry, renting movies is not available for " << tenant->get_name() <<" at this time"<<endl;
		return 0;
	}
	
	// check if the customer is not a member to pay deposite
	int cash_paid = 0;
	if(!tenant->is_member)
	{
		cout<<endl<<"fees is "<<rented_movie_cd->get_movie_title()->get_fees()<<" and MUST be paid deposit"<<endl;
		cout<<"Pay? press (y) for yes";
		if (_getch()=='y')
			cash_paid=rented_movie_cd->get_movie_title()->get_fees();
		else
		{
			accounts_admin_->delete_n(phone);
			return 0;
		}
	}
	else
	{
		cout<<endl<<"Do you want to pay deposit ? press (y) for yes "<<endl;
		if (_getch()=='y')
	{
		cout <<" amount: ";
		cin>>cash_paid;
	}
	}
	
	cout<<endl<<endl<<"Summary of rental information:"<<endl;
	cout<<"tenant: "<<tenant->get_name()<<endl;
	cout<<"Mobile: "<<tenant->get_phone()<<endl;
	cout<<"Rented movie: "<<rented_movie_cd->get_movie_title()->get_name()<<endl;
	cout<<"Confirm ? press (y) for yes";
	if (_getch()!='y')
	{
		accounts_admin_->delete_n(phone);
		return 0;
	}

	rentals.push_back(rental(tenant, rented_movie_cd));

	// set movie_cd is rented
	rented_movie_cd->is_rented=true;

	// set customer rents_now
	tenant->rents_now=true;

	// decrement number of available copies
	movies_admin_->dec_copies_available(movie,1);

	// set fees paid deposit
	rentals[rentals_counter].get_fees()->set_fees_paid(cash_paid);

	// increase rentals counter
	rentals_counter= rentals_counter+1;

	return 1;
};

bool rental_admin::valid_rental(int phone)
{
	for ( int i=0; i<rentals_counter; i++)
		{
			if (rentals[i].get_customer()->get_phone()==phone)
			{
				return true;
			}
			
		};
	return false;
};

rental *rental_admin::get_rental(int phone)
{
		for ( int i=0; i<rentals_counter; i++)
		{
			if (rentals[i].get_customer()->get_phone()==phone)
			{
				return &rentals[i];
			}
		};
};

bool rental_admin::check_late(rental *rental_)
{
	if (rental_->get_returned_date()->checkless())
	{
	return false;
	}
	return true;
};

void rental_admin::return_(int phone)
{
	rental_admin::get_rental(phone)->get_movie()->is_rented=false;
	rental_admin::get_rental(phone)->get_movie()->get_movie_title()->inc_copies_available(1);

	for ( int i=0; i<rentals_counter; i++)
		{
			if (rentals[i].get_customer()->get_phone()==phone)
			{
				rentals.erase(rentals.begin()+i);
			}
		};
	accounts_admin *accounts_admin_=accounts_admin::get_accounts_admin();
	if(accounts_admin_->search_m(phone))
	{
		accounts_admin_->get_tenant(phone)->rents_now=false;
	}
	accounts_admin_->delete_n(phone);
};

int rental_admin::calculate_left_fees(rental *rental)
{
	return rental->get_fees()->get_amount() - rental->get_fees()->get_fees_paid();
};